#include <iostream>

class Animal
{
public:
    virtual void speak()
    {
        std::cout<<"动物叫"<<std::endl;
    }
    virtual ~Animal()   // 虚析构函数
    {
        std::cout<<"父类析构函数\n";
    }
};

class Dog:public Animal
{
public:
   void speak()
   {
    std::cout<<"汪汪汪"<<std::endl;
   }
   ~Dog()
   {
    std::cout<<"子类析构\n";
   }
};

class Cat:public Animal
{
public:
   void speak()
   {
    std::cout<<"喵喵喵"<<std::endl;
   }
   ~Cat()
   {
    std::cout<<"子类析构\n";
   }
};

void Show(Animal *a)
{
    a->speak();
}

int main()
{
    Animal *a = new Dog;  // 声明的是指向子类的指针
    Animal *b = new Cat;

    Show(a);
    Show(b);

    delete a;  
    delete b;  // 释放父类析构函数 若子类有申请的内存便会漏掉 所以父类析构需要声明虚析构

    return 0;
}
